package com.example.eduservice.service.impl;

import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.EduCourseDescription;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.mapper.EduCourseMapper;
import com.example.eduservice.service.EduChapterService;
import com.example.eduservice.service.EduCourseDescriptionService;
import com.example.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.servicebase.exceptionhandler.GuiliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节和章节service
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService chapterService;

    //添加课程基本信息的方法
    @Override

    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表里添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);//把courseInfoVo的值设置到eduCourse
        int insert = baseMapper.insert(eduCourse);//会返回成功添加了几条记录

        if(insert ==0){
            //添加失败
            throw new GuiliException(20001,"添加课程信息失败");//抛异常

        }

        //获取添加之后课程id
        String cid=eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid); //设置描述（简介）id就是课程id
        courseDescriptionService.save(courseDescription);//传一个描述（简介）对象
        return cid;
    }

    //根据课程id查询课程的基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查询简介表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new GuiliException(20001,"修改课程信息失败");
        }

        //2 修改简介表
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper，在这里面调方法可以写baseMapper或者this，但是调用自己写的mapper必须要用baseMapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);//baseMapper.getPublishCourseInfo(id).var
        return publishCourseInfo;
    }

    //删除课程方法
    @Override
    public void removeCourse(String courseId) {
        //1、根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2、根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3、根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4、根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0){//失败返回
            throw new GuiliException(2001,"删除失败");
        }
    }
}
